package com.example.zzl.view.activity.UserActivity;

import android.content.DialogInterface;
import android.database.Cursor;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.databinding.ActivityRegistBinding;
import com.example.zzl.gen.UserDao;
import com.example.zzl.model.bean.User;
import com.example.zzl.utils.GreenDaoManager;

import java.util.ArrayList;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener{

    private ActivityRegistBinding binding;
    private TextView tv_regist,tv_send;
    private EditText et_phone,et_code,et_nickName,et_pwd;
    private UserDao userDao;
    private int count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        binding = DataBindingUtil.setContentView(this,R.layout.activity_regist);
        init();
    }

    private void init() {
        binding.setMyClick(this);
        tv_regist = binding.tvRegistFinish;
        tv_send = binding.tvYanzhengRegist;
        et_phone = binding.etPhoneRegist;
        et_code = binding.etYanzhengRegist;
        et_nickName = binding.etNicknameRegist;
        et_pwd = binding.etPwdRegist;
        userDao = GreenDaoManager.getInstance(this).getNewSession().getUserDao();

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.tv_yanzheng_regist:
                break;
            case R.id.tv_regist_finish:
                //Log.e("data", et_phone.getText().toString());
                if(TextUtils.isEmpty(et_phone.getText()) || TextUtils.isEmpty(et_nickName.getText()) || TextUtils.isEmpty(et_pwd.getText())){
                    Toast.makeText(this, "请填写完整注册信息", Toast.LENGTH_SHORT).show();
                }else{
                    ArrayList<User> list = (ArrayList<User>) userDao.loadAll();
                    //Log.e("data",list.size()+">>>>");
                    for (int i = 0; i < list.size(); i++) {
                        if(et_phone.getText().toString().equals(list.get(i).getPhoneNum()) || et_nickName.getText().toString().equals(list.get(i).getNickName())){
                            count+=1;
                            if(et_phone.getText().toString().equals(list.get(i).getPhoneNum())){
                                Toast.makeText(this, "手机号已被注册", Toast.LENGTH_SHORT).show();
                                return;
                            }else if(et_nickName.getText().toString().equals(list.get(i).getNickName())){
                                Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
                            }
                            return;
                        }
                    }
                    if(count == 0){
                        User user = new User("",et_nickName.getText().toString(),et_pwd.getText().toString(),et_phone.getText().toString(),"","成功");
                        userDao.insert(user);
                        AlertDialog.Builder builder = new AlertDialog.Builder(RegistActivity.this);
                        builder.setTitle("注册成功").setMessage("请选择跳转").setNegativeButton("自动登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(RegistActivity.this, "6646464", Toast.LENGTH_SHORT).show();
                                MyApplication.nichName = et_nickName.getText().toString();

                            }
                        }).setPositiveButton("返回登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                setResult(123,getIntent());
                                finish();
                            }
                        }).create().show();
                    }
                    count = 0;
                }
                break;
            case R.id.tv_xieyi:
                Toast.makeText(this, "用户协议", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_regist_close:
                setResult(222,getIntent());
                finish();
                break;
        }
    }
}
